<?php
use Illuminate\Http\JsonResponse;
use App\Enums\Api\Response;
use App\Models\Category\CategoryModule;
use App\Models\Tool\FileUrl;
use App\Models\Category\Category;
function apiResponse(int $code = 200 ,  $data = [],string $msg='',string $type=''):JsonResponse
{
    if ($data!=[])
        return response()->json([
            'success'=>true,
            'type'=>$type,
            'message'=>$msg,
            'data'=>$data,
        ])->setStatusCode($code);
    if ($code >=  Response::SERVICE_SUCCESS->value) {
        return response()->json([
            'code' => $code,
            'success' => false,
            'message' => $msg
        ], $code== Response::SERVICE_SUCCESS->value?401:403);
    }
    return response()->json([
        'success'=>true,
        'type'=>$type,
        'message'=>$msg
    ])->setStatusCode($code);
}
/**
 * 资源加载方式设计（已有）
 * 域名+资源地址+(年级module标识)+（如果开启目录则加目录id如3001）+（如果开启类型加类型 图片picture 音频audio 视频video）
 **/
function convert_file_address_exist(int $module,string $file_address,string $grade,string|null $category='',string $type='',object|array $file_url=null):string
{
    if (empty($file_address)) {
        return '';
    }
    if (!in_array($module,array_keys(CategoryModule::$moduleFileMap))){
        return '';
    }
    return extracted($file_url, $grade, $category??'', $type, $file_address);
}

/**
 * @param object|array|null $file_url
 * @param string $grade
 * @param string $category
 * @param string $type
 * @param string $file_address
 * @return string
 */
function extracted(object|array|null $file_url, string $grade, string $category, string $type, string $file_address): string
{
    if (!$file_url) {
        return '';
    }
    $url = Str::of($file_url->domain)->trim('/')->toString() . '/' ;
    if ($file_url->url != '' && $file_url->url != null){
        $url.= Str::of($file_url->url)->trim('/')->toString() . '/' ;
    }
    if (is_numeric($grade)){
        $grade=Category::query()->where('id',$grade)->value('module');
    }
    if ($grade != '' && $grade != null){
        $url.= Str::of($grade)->trim('/')->toString() . '/' ;
    }
    if ($file_url->sign!='' && $file_url->sign != null){
        $url.=  Str::of($file_url->sign)->trim('/')->toString() . '/';
    }

    if ($file_url->is_category == FileUrl::IS_CATEGORY_ON) {
        $category_url=Str::of($category)->trim('/')->toString();
        if ($category_url!=''){
            $url .= Str::of($category)->trim('/')->toString() . '/';
        }
    }

    $suffix='.m3u8';
    if ($file_url->is_type == FileUrl::IS_TYPE_ON) {
        switch ($type) {
            case '.png':
                $url .= FileUrl::IMAGE_SIGN . '/';
                break;
            case '.mp3':
                $url .= FileUrl::AUDIO_SIGN . '/';
                break;
            case '.mp4':
                $url .= FileUrl::VIDEO_SIGN . '/';
                break;
        }
    }
    switch ($type) {
        case '.png':
            $suffix='.png';
            break;
        case '.mp4':
        case '.mp3':
            $suffix='.m3u8';
            $file_address.='/'.$file_address;
            break;
    }
    $url .= $file_address;

    return $url.$suffix;
}
